package com.wantup.miaosha.controller;

import com.wantup.miaosha.entity.TblUser;
import com.wantup.miaosha.pojo.RespInfo;
import com.wantup.miaosha.pojo.UserPojo;
import com.wantup.miaosha.service.UserService;
import com.wantup.miaosha.utils.MD5Util;
import com.wantup.miaosha.utils.RedisUtils;
import com.wantup.miaosha.utils.UUIDUtil;
import com.wantup.miaosha.utils.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/6/4 15:49
 */
@Controller
@RequestMapping(value = "/login")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtils;


    @RequestMapping(value = "/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * https://blog.csdn.net/fuzeng_Xu/article/details/80888125
     *
     * @param userPojo
     * @return
     */
    @PostMapping(value = "/doLogin", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    //@PostMapping(value = "/doLogin")
    @ResponseBody
    public RespInfo doLogin(@Validated @RequestBody UserPojo userPojo, HttpServletResponse response) {
        log.info("前端ajax的提交 --> " + "\n" + userPojo);
        RespInfo respInfo = new RespInfo();
        //验证参数
        try {
            String name = userPojo.getName();
            String password = userPojo.getPassword();
            if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
                //用户名 密码不能为空
                return respInfo.setCode(2).setMessage("用户名密码不能为空").setData("");
            }

            //验证手机号
            if (!ValidatorUtil.isMobile(name)) {
                return respInfo.setCode(3).setMessage("手机号格式不正确").setData("");
            }

            //登陆
            TblUser tblUser = this.loginMethod(userPojo);
            if (tblUser == null) {
                return respInfo.setCode(4).setMessage("用户不存在").setData("");
            }
            log.info("数据库中 用户信息 --> " + tblUser);
            //验证密码
            String salt = tblUser.getSalt();
            String passToDBPass = MD5Util.inputPassToDBPass(password, salt);
            String passwordDB = tblUser.getPassword();
            if (!passToDBPass.equals(passwordDB)) {
                return respInfo.setCode(1).setMessage("密码错误").setData("");
            }
            //生成一个session 写入到cookie中存到客户端。
            String token = UUIDUtil.uuid();
            this.redisUtils.set(token, tblUser);
            Cookie cookie = new Cookie("token", token);
            cookie.setMaxAge(3600 * 24 * 2);
            cookie.setPath("/");

            response.addCookie(cookie);

            return respInfo.setCode(0).setMessage("登陆成功").setData("");


        } catch (Exception e) {
            e.printStackTrace();
            respInfo.setCode(-1).setMessage("登录异常").setData("");
        }
        log.info("响应给前端 --> " + "\n" + respInfo);
        return respInfo;
    }


    private TblUser loginMethod(UserPojo userPojo) {
        if (userPojo == null) {
            return null;
        }
        String name = userPojo.getName();
        String password = userPojo.getPassword();
        TblUser tblUser = this.userService.getByUserId(name);
        return tblUser;
    }

}
